﻿using ArgusLight.Fractals2D;
using System;
using System.Numerics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Reflection;
using System.IO;
using System.IO.IsolatedStorage;
using ArgusControls;
using ImageTools;

namespace Fractals
{
	public partial class MainPage : UserControl
	{
		#region Fields
		Fractal fractal;
		//Fractal previewFractal;
		CalculationProgressWindow progressWindow;
		SelectColoringWindow selectColoringWindow;
		ControlFractalParameter controlFractalParameter;
		UIColoringParameter uiColoringParameter;
		SaveFileDialog SFD;
		SaveFileDialog exportImageSFD;
		OpenFileDialog OFD;
		SaveFileDialog SFDScreenShot;
		FractalParameter fParas;
		bool addToHistory = false;
		PreviewManager previewManager;
		//PreviewWindow previewWindow;
		#endregion

		#region Initialization
		public MainPage()
		{
			InitializeComponent();
			this.cbZoomMode.SetEnumType(typeof(ZoomMode));
			this.cbZoomMode.Select(FractalImage.SettingsStr.Standard.ZoomMode);
			this.InitializeStorage();

			this.fractalImage.ZoomRequest += this.fractalImage_ZoomRequest;
			this.fractalImage.PreviewRectChanged += fractalImage_PreviewRectChanged;
			this.controlFractalParameter = new ControlComplexFractalParameter();
			this.borderControlFractalParameter.Child = this.controlFractalParameter.GetUIElement();
			this.uiColoringParameter = new UIColoringParameter();

			this.progressWindow = new CalculationProgressWindow();
			this.progressWindow.Width = 400;
			this.progressWindow.CancelRequested += new RoutedEventHandler(this.progressWindow_CancelRequested);
			this.progressWindow.KeyUp += this.UserControl_KeyUp_1;

			this.selectColoringWindow = new SelectColoringWindow();
			this.selectColoringWindow.Closed += selectColoringWindow_Closed;
			this.selectColoringWindow.KeyUp += this.UserControl_KeyUp_1;

			//this.previewWindow = new PreviewWindow();
			//this.previewWindow.Parent = this.LayoutRoot;

			this.SFD = new SaveFileDialog();
			this.SFD.DefaultExt = "png";
			this.SFD.Filter = "Image (*.png)|*.png|Data (*.fxml)|*.fxml";
			this.OFD = new OpenFileDialog();
			this.OFD.Multiselect = false;
			this.OFD.Filter = "Data (*.fxml)|*.fxml";
			this.SFDScreenShot = new SaveFileDialog();
			this.SFDScreenShot.DefaultExt = "png";
			this.SFDScreenShot.Filter = "Image (*.png)|*.png|Data (*.fxml)|*.fxml";

			this.previewManager = new PreviewManager();
			this.previewManager.PreviewImageChanged += previewManager_PreviewImageChanged;

			this.progressWindow.Opened += progressWindow_Opened;
			//this.parameterHistory = new Stack<FractalParameter>();

			App.Current.Exit += App_Exit;

			this.InitializeStartImage();
		}

		void App_Exit(object sender, EventArgs e)
		{
			IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
			settings[SettingNames.PreviewWindowBoundaryRect] = this.previewWindow.BoundaryRect;
		}

		void previewManager_PreviewImageChanged(object sender, RoutedPropertyChangedEventArgs<BitmapSource> e)
		{
			this.previewWindow.previewImage.Source = e.NewValue;
			//this.previewImage.Source = e.NewValue;
		}

		void InitializeStartImage()
		{
			this.fractalImage.Width = 450;
			this.fractalImage.Height = 300;
			this.InitializeFractal(new MandelbrotFractal((int)this.fractalImage.Width, (int)this.fractalImage.Height, 1000, null));
			this.SetColoring(new ArgusLight.Fractals2D.ComplexFractalColoring.EscapeTime.HSV1());
		}

		void InitializeFractal(Fractal fractal)
		{
			this.fractal = fractal;
			this.fractal.CalculationFinished += new CalculationFinishedHandler(this.fractal_CalculationFinished);
			this.fractal.CalculationAborted += new CalculationAbortedHandler(this.fractal_CalculationAborted);
			this.fractal.ProgressUpdated += new ProgressUpdatedHandler(this.progressWindow.UpdateProgress);
			//this.parameterHistory = new Stack<FractalParameter>();
		}

		void InitializeStorage()
		{
			FieldInfo[] fis = typeof(IsolatedDirectories).GetFields(BindingFlags.Public | BindingFlags.Static);
			using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
			{
				//if (store.DirectoryExists(IsolatedDirectories.Favorites) == false)
				//	store.CreateDirectory(IsolatedDirectories.Favorites);

				//if (store.DirectoryExists(IsolatedDirectories.History) == false)
				//	store.CreateDirectory(IsolatedDirectories.History);
				foreach (FieldInfo fi in fis)
				{
					string path = (string)fi.GetValue(null);
					if (store.DirectoryExists(path) == false)
						store.CreateDirectory(path);
				}
			}

			IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
			if (settings.Contains(SettingNames.MaxHistoryLength) == false)
				settings.Add(SettingNames.MaxHistoryLength, 100);
			if (settings.Contains(SettingNames.History) == false)
				settings.Add(SettingNames.History, new string[] { });

			if (settings.Contains(SettingNames.PreviewWindowBoundaryRect) == true)
				this.previewWindow.BoundaryRect = (Rect)settings[SettingNames.PreviewWindowBoundaryRect];

			HistoryManager.CanGoBackChanged += HistoryManager_CanGoBackChanged;
			HistoryManager.Initialize();
		}
		#endregion

		#region Event Subscriptions
		#region UserControl Events
		private void UserControl_Loaded(object sender, RoutedEventArgs e)
		{
			if (HistoryManager.CanGoBack == true)
			{
				ArgusControls.MessageBox.Show(
					Localization.Text.LoadPreviousPromptMessage,
					Localization.Text.LoadPreviousPromptTitle,
					MessageBoxButtons.YesNo,
					result =>
					{
						if (result == MessageBoxResults.Yes)
						{
							FractalSaveStruct str = HistoryManager.GetCurrent();
							this.InitializeFractal(str.GetFractal());
							this.StartCalculation(str.FractalParameter, false);
						}
						else
						{
							this.StartCalculation(this.fractal.Parameter, true);
						}
					});
			}
			else if (HistoryManager.GetCount() == 0)
			{
				this.StartCalculation(this.fractal.Parameter, true);
			}
			else
			{
				this.StartCalculation(this.fractal.Parameter, false);
			}
			//this.previewWindow.Show();
			//this.previewWindow.Margin = new Thickness()
			//{
			//	Top = this.GUIStackPanel.Margin.Top,
			//	Left = this.GUIStackPanel.Margin.Left + this.GUIStackPanel.Margin.Right + this.GUIStackPanel.ActualWidth
			//};
			this.previewWindow.Show();
		}

		private void UserControl_KeyUp_1(object sender, KeyEventArgs e)
		{
			if (Keyboard.Modifiers == ModifierKeys.Control)
			{
				if (e.Key == Key.S)
				{
					this.SaveImage();
				}
				else if (e.Key == Key.P)
				{
					UIElement[] elements = null;
					if (sender != this && sender is UIElement)
						elements = new UIElement[] { (UIElement)sender };
					this.SaveScreenShot(elements);
				}
				else if (e.Key == Key.Add)
				{
					this.bAddFavorite_Click(null, null);
				}
				else if (e.Key == Key.Subtract)
				{
					this.bLoadFavorite_Click(null, null);
				}
				else if (e.Key == Key.O)
				{
					this.bOpen_Click(null, null);
				}
				else if (e.Key == Key.C)
				{
					this.bSelectColoring_Click(null, null);
				}
				else if (e.Key == Key.Back)
				{
					this.bMenuEditBack_Click(null, null);
				}
			}
		}
		#endregion

		#region Window-Events
		void progressWindow_Opened(object sender, System.EventArgs e)
		{
			this.fractal.StartCalculation(this.fParas);
		}

		void selectColoringWindow_Closed(object sender, System.EventArgs e)
		{
			if (this.selectColoringWindow.DialogResult == false)
				return;

			this.SetColoring(this.selectColoringWindow.SelectedColoring);

			BitmapSource src = this.fractal.GetImage();
			if (src == null)
				this.StartCalculation(this.fractal.Parameter, false);
			else
				this.fractalImage.Source = src;
		}

		private void progressWindow_CancelRequested(object sender, RoutedEventArgs e)
		{
			this.fractal.RequestCalculationCancelation();
		}

		void ExportImageWindow_Closed(object sender, System.EventArgs e)
		{
			ExportImageWindow Sender = (ExportImageWindow)sender;
			if (Sender.DialogResult == false)
			{
				this.bExport.IsEnabled = true;
				this.bExport.Text = Localization.Text.MenuFileExport;
				return;
			}
			this.exportImageSFD = Sender.SaveFileDialog;
			Fractal f = Sender.Fractal;
			f.ProgressUpdated += this.ExportImageFractal_ProgressUpdated;
			f.CalculationFinished += this.ExportImageFractal_CalculationFinished;
			f.StartCalculation(Sender.FractalParameter);
		}

		void LoadFavoriteWindow_Closed(object sender, System.EventArgs e)
		{
			FavoritesWindow Sender = (FavoritesWindow)sender;
			if (Sender.DialogResult == false)
				return;

			Fractal f = Sender.StorageStruct.GetFractal();
			this.InitializeFractal(f);
			this.SetColoring(f.Coloring);
			this.StartCalculation(Sender.StorageStruct.FractalParameter, true);
		}
		#endregion

		#region FractalImage Events
		private void fractalImage_MouseMove(object sender, MouseEventArgs e)
		{
			if (this.fractal.GetType().IsSubclassOf(typeof(ComplexFractal)) == false)
				return;
			if (this.fractal.IsCalculating == true)
				return;
			ComplexFractal f = (ComplexFractal)this.fractal;
			Point p = e.GetPosition(this.fractalImage);
			Complex c = f.ScreenToComplex(p);
			this.textMouseCoordsValue.Text = c.ToString();
		}

		void fractalImage_PreviewRectChanged(object sender, RoutedPropertyChangedEventArgs<Rect> e)
		{
			if (e.NewValue == Rect.Empty)
			{
				//if (this.fractal.IsCalculating == false)
				//	this.previewWindow.Close();
					//this.borderPreviewImage.Visibility = System.Windows.Visibility.Collapsed;
				return;
			}

			if (e.OldValue == Rect.Empty)
			{
				//this.previewWindow.Show();
				////this.borderPreviewImage.Visibility = System.Windows.Visibility.Visible;
				//this.UpdateLayout();
			}

			FractalParameter p = this.GetFractalParameter(e.NewValue);
			Size size = new Size(p.PixelWidth, p.PixelHeight);
			size = this.previewWindow.ClientSize.ChangeRatio(size.GetRatio(), System.Windows.ExtensionMethods.ChangeRatioOptions.DecreaseAreaSize);
			p.PixelWidth = (int)size.Width;
			p.PixelHeight = (int)size.Height;
			this.previewWindow.previewImageCanvas.Width = p.PixelWidth;
			this.previewWindow.previewImageCanvas.Height = p.PixelHeight;
			this.previewWindow.previewImageBackground.Source = this.fractalImage.Source.Crop(e.NewValue);
			this.previewWindow.previewImage.Source = null;
			this.previewManager.AbortAll();

			System.Windows.Threading.DispatcherTimer timer = new System.Windows.Threading.DispatcherTimer();
			timer.Interval = new TimeSpan(0, 0, 0, 0, 500);
			timer.Tick += (a, b) =>
				{
					if (e.NewValue == this.fractalImage.PreviewRect)
						this.previewManager.StartCalculation(this.fractal.GetType(), this.fractal.Coloring.GetType(), p, this.fractal.Coloring.Parameter);
					timer.Stop();
				};
			timer.Start();
		}

		private void fractalImage_ZoomRequest(Rect ZoomArea)
		{
			FractalParameter p = this.GetFractalParameter(ZoomArea);
			if (p == null)
				return;
			Fractal prev = this.previewManager.Fractal;
			if (prev != null)
			{
				FractalParameter prevP = prev.Parameter;
				if (prevP.PixelWidth == p.PixelWidth && prevP.PixelHeight == p.PixelHeight)
				{
					this.InitializeFractal(prev);
					if (this.previewManager.IsFinished == true)
					{
						this.addToHistory = true;
						this.fractal_CalculationFinished(this.fractal, new CalculationFinishedArgs());
						return;
					}
				}
			}
			this.StartCalculation(p, true);
		}

		private FractalParameter GetFractalParameter(Rect ZoomArea)
		{
			if (this.fractal.GetType().IsSubclassOf(typeof(ComplexFractal)) == false)
				return null;
			ComplexFractal f = (ComplexFractal)this.fractal;
			ComplexFractalParameter para = f.Parameter;
			Rect Screen = new Rect(0, 0, this.fractalImage.Width, this.fractalImage.Height);
			para.ShownArea = ComplexFractal.ScreenToComplex(Screen, para.ShownArea, ZoomArea, para.Rotation);
			FractalParameter p = para;
			this.controlFractalParameter.GetValues(ref p);

			Size s = new Size(p.PixelWidth, p.PixelHeight);
			if (s.GetRatio() != ZoomArea.GetRatio())
			{
				s = s.ChangeRatio(ZoomArea.GetRatio(), System.Windows.ExtensionMethods.ChangeRatioOptions.KeepAreaSize);
				p.PixelWidth = (int)s.Width;
				p.PixelHeight = (int)s.Height;
			}
			return p;
		}

		private void fractalImage_MouseLeave(object sender, MouseEventArgs e)
		{
			this.textMouseCoordsValue.Text = Localization.Text.textMouseCoordinatesValue;
		}
		#endregion

		#region Fractal Calculation Events
		private void fractal_CalculationAborted(object sender, CalculationAbortedArgs e)
		{
			this.progressWindow.Close();
		}

		private void fractal_CalculationFinished(object sender, CalculationFinishedArgs e)
		{
			this.fractalImage.Width = this.fractal.Width;
			this.fractalImage.Height = this.fractal.Height;
			this.fractalImage.Source = this.fractal.GetImage();
			this.progressWindow.bCancel.IsEnabled = true;
			this.controlFractalParameter.SetValues(this.fractal.Parameter);
			this.uiColoringParameter.SetValues(this.fractal.Coloring.Parameter);
			this.progressWindow.Close();
			//this.previewWindow.Close();
			//this.borderPreviewImage.Visibility = System.Windows.Visibility.Collapsed;
			//this.progressWindow.UpdateLayout();

			if (this.addToHistory == true)
				HistoryManager.Add(this.fractal);
			this.addToHistory = false;

#if DEBUG
			if (this.boolVideo == true)
				this.CreateTestVideo();
#endif
		}

		void ExportImageFractal_CalculationFinished(object sender, CalculationFinishedArgs e)
		{
			Fractal Sender = (Fractal)sender;
			ExtendedImage im = (new WriteableBitmap(Sender.GetImage())).ToImage();
			Stream file = this.exportImageSFD.OpenFile();
			im.WriteToStream(file);
			file.Close();
			this.bExport.IsEnabled = true;
			this.bExport.Text = Localization.Text.MenuFileExport;
		}

		void ExportImageFractal_ProgressUpdated(object sender, ProgressUpdatedArgs e)
		{
			this.bExport.Text = e.Progress.ToString() + "%";
		}
		#endregion

		#region Button Click Events
		private void bSave_Click(object sender, RoutedEventArgs e)
		{
			this.SaveImage();
		}

		private void bCalculate_Click(object sender, RoutedEventArgs e)
		{
			FractalParameter para = this.fractal.Parameter;
			this.controlFractalParameter.GetValues(ref para);
			this.StartCalculation(para, true);
		}

		private void bSelectColoring_Click(object sender, RoutedEventArgs e)
		{
			this.selectColoringWindow.Show(this.fractal.Parameter, typeof(ArgusLight.Fractals2D.ComplexFractalColoring.Coloring), this.fractal.GetType());
		}

		private void bExport_Click(object sender, RoutedEventArgs e)
		{
			this.bExport.IsEnabled = false;
			ExportImageWindow window = new ExportImageWindow();
			window.Closed += this.ExportImageWindow_Closed;
			window.KeyUp += this.UserControl_KeyUp_1;
			window.Show(this.fractal.GetType(), this.fractal.Coloring.GetType(), this.fractal.Parameter);
		}

		private void bMenuEditBack_Click(object sender, RoutedEventArgs e)
		{
			FractalSaveStruct str = HistoryManager.Pop();
			this.InitializeFractal(str.GetFractal());
			this.StartCalculation(str.FractalParameter, false);
		}

		private void bAddFavorite_Click(object sender, RoutedEventArgs e)
		{
			FavoritesWindow w = new FavoritesWindow();
			w.KeyUp += this.UserControl_KeyUp_1;
			w.ShowSaveDialog(this.fractal);
		}

		private void bLoadFavorite_Click(object sender, RoutedEventArgs e)
		{
			FavoritesWindow LoadFavoriteWindow = new FavoritesWindow();
			LoadFavoriteWindow.KeyUp += this.UserControl_KeyUp_1;
			LoadFavoriteWindow.Closed += LoadFavoriteWindow_Closed;
			LoadFavoriteWindow.ShowOpenDialog();
		}

		bool boolVideo = false;
		private void bOpen_Click(object sender, RoutedEventArgs e)
		{
			if (this.OFD.ShowDialog() == false)
				return;

			FractalSaveStruct str;
			using (FileStream file = this.OFD.File.OpenRead())
			{
				str = FractalSaveStruct.FromStream(file);
				file.Close();
			}

			Fractal f = str.GetFractal();
			this.InitializeFractal(f);
			this.boolVideo = true;
			this.StartCalculation(str.FractalParameter, true);
		}

		private void bMenuEditClearHistory_Click(object sender, RoutedEventArgs e)
		{
			ArgusControls.MessageBox.Show(
				Localization.Text.bClearHistoryPromptMessage,
				Localization.Text.bClearHistoryPromptTitle,
				ArgusControls.MessageBoxButtons.YesNo,
				result =>
				{
					if (result != MessageBoxResults.Yes)
						return;
					HistoryManager.ClearHistory();
					HistoryManager.Add(this.fractal);
				});
		}

		private void bMenuEditLoadStartimage_Click(object sender, RoutedEventArgs e)
		{
			this.InitializeStartImage();
			this.StartCalculation(this.fractal.Parameter, true);
		}

		private void bMenuHelpKeyShortCuts_Click(object sender, RoutedEventArgs e)
		{
			HelpWindowKeystrokeCombinations window = new HelpWindowKeystrokeCombinations();
			window.KeyUp += this.UserControl_KeyUp_1;
			window.Show();
		}
		#endregion

		#region CheckBox Events
		private void cbKeepRatio_Checked(object sender, RoutedEventArgs e)
		{
			if (this.fractalImage == null)
				return;
			FractalImage.SettingsStr sett = this.fractalImage.Settings;
			sett.KeepRatio = (bool)this.cbKeepRatio.IsChecked;
			this.fractalImage.Settings = sett;
		}

		private void cbKeepRatio_Unchecked(object sender, RoutedEventArgs e)
		{
			if (this.fractalImage == null)
				return;
			FractalImage.SettingsStr sett = this.fractalImage.Settings;
			sett.KeepRatio = (bool)this.cbKeepRatio.IsChecked;
			this.fractalImage.Settings = sett;
		}

		private void cbZoomMode_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			if (this.fractalImage == null)
				return;
			FractalImage.SettingsStr s = this.fractalImage.Settings;
			s.ZoomMode = (ZoomMode)this.cbZoomMode.SelectedEnum;
			this.fractalImage.Settings = s;
		}
		#endregion

		#region Misc
		void controlColoringParameter_RedrawRequested(object sender, EventArgs e)
		{
			UIColoringParameter Sender = (UIColoringParameter)sender;
			ColoringParameter para = this.fractal.Coloring.Parameter;
			Sender.GetValues(ref para);
			this.fractal.Coloring.Parameter = para;
			this.fractalImage.Source = this.fractal.GetImage();
		}

		void HistoryManager_CanGoBackChanged(object sender, RoutedPropertyChangedEventArgs<bool> e)
		{
			this.bMenuEditBack.IsEnabled = e.NewValue;
			this.bMenuEditClearHistory.IsEnabled = e.NewValue;
		}
		#endregion
		#endregion

		#region Private Methods
		private void SetColoring(Coloring c)
		{
			this.uiColoringParameter.RedrawRequested -= this.controlColoringParameter_RedrawRequested;

			if (c is ArgusLight.Fractals2D.ComplexFractalColoring.RouteLengthBased.UserColoring)
			{
				this.uiColoringParameter = new UIRouteLengthUserColoringParameter();
			}
			else if (c is ArgusLight.Fractals2D.ComplexFractalColoring.PeriodicColoring)
			{
				this.uiColoringParameter = new UIPeriodicColoringParameter();
			}
			else
			{
				this.uiColoringParameter = new UIColoringParameter();
			}

			this.borderControlColoringParameter.Child = this.uiColoringParameter;
			this.bSelectColoring.Text = Localization.Colorings.ResourceManager.GetString(c.GetType().Name);
			if (c == this.selectColoringWindow.SelectedColoring)
				this.bSelectColoring.ImageSource = this.selectColoringWindow.PreviewImage;
			
			this.fractal.SetColoring(c);

			this.uiColoringParameter.SetValues(this.fractal.Coloring.Parameter);
			this.uiColoringParameter.RedrawRequested += this.controlColoringParameter_RedrawRequested;
		}

		private void SaveImage()
		{
			BitmapSource bs = this.fractal.GetImage();
			if (bs == null)
				return;

			if (this.SFD.ShowDialog() == false)
				return;
			using (System.IO.Stream file = this.SFD.OpenFile())
			{
				if (this.SFD.SafeFileName.EndsWith(".png") == true)
				{
					ExtendedImage im = (new WriteableBitmap(bs)).ToImage();
					im.WriteToStream(file);
				}
				else if (this.SFD.SafeFileName.EndsWith(".fxml") == true)
				{
					FractalSaveStruct str = new FractalSaveStruct(this.fractal);
					str.Save(file);
				}
				file.Close();
			}
		}

		private void SaveScreenShot(UIElement[] Render)
		{
			WriteableBitmap wb = new WriteableBitmap(this.LayoutRoot, null);
			if (Render != null)
			{
				foreach (UIElement ele in Render)
				{
					GeneralTransform gt = ele.TransformToVisual(this.LayoutRoot);
					TranslateTransform tt = new TranslateTransform();
					Point p = gt.Transform(new Point(0, 0));
					tt.X = p.X;
					tt.Y = p.Y;
					wb.Render(ele, tt);
				}
			}

			if (this.SFDScreenShot.ShowDialog() == false)
				return;

			using (Stream file = this.SFDScreenShot.OpenFile())
			{
				ExtendedImage im = wb.ToImage();
				im.WriteToStream(file);
				file.Close();
			}
		}

		private void StartCalculation(FractalParameter para, bool addToHistory)
		{
			this.addToHistory = addToHistory;
			this.fParas = para;
			//this.progressWindow.SetPercentage(0);
			this.progressWindow.Show();
			//this.controlColoringParameter.GetValues(ref this.fractal.Coloring.Parameter);
			//this.fractal.StartCalculation(para);
		}
		#endregion

		#region Overriden Methods
		protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
		{
			e.Handled = true;
			base.OnMouseRightButtonDown(e);
		}
		#endregion

		private void CreateTestVideo()
		{
#if false
			using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
			{
				string name = "Test.y4m";
				if (store.FileExists(name) == true)
					store.DeleteFile(name);

				using (IsolatedStorageFileStream file = store.CreateFile(name))
				{
					ArgusMedia.Video.Y4MEncoder encoder = new ArgusMedia.Video.Y4MEncoder(
						this.fractal.Width,
						this.fractal.Height,
						new ArgusMedia.Video.Rational(5, 1),
						ArgusMedia.Video.Y4MColorSpace.C444,
						ArgusMedia.Video.KrKbConstants.ITU_R_BT_601);

					//double translation = 0;
					//for (int i = 0; i < 50; i++)
					//{
					//	WriteableBitmap wb = new WriteableBitmap(this.fractal.GetImage());
					//	encoder.AddImage(wb, file);
					//	translation += i/25.0;
					//	if (translation >= 1)
					//		translation = 0;
					//	ArgusLight.Fractals2D.ComplexFractalColoring.PeriodicColoringParameter para =
					//		(ArgusLight.Fractals2D.ComplexFractalColoring.PeriodicColoringParameter)this.fractal.Coloring.Parameter;
					//	para.Translation = translation;
					//	this.fractal.Coloring.Parameter = para;
					//}
					for (int i = 0; i < 60; i++)
					{
						ArgusLight.Fractals2D.ComplexFractalColoring.PeriodicColoringParameter para =
							(ArgusLight.Fractals2D.ComplexFractalColoring.PeriodicColoringParameter)this.fractal.Coloring.Parameter;
						para.NumberOfColorPeriods = i+1;
						this.fractal.Coloring.Parameter = para;
						WriteableBitmap wb = new WriteableBitmap(this.fractal.GetImage());
						encoder.AddImage(wb, file);
					}
					file.Close();
				}
			}
#endif
		}
	}
}